package com.coffee.soft.service.impl;

import com.coffee.soft.entity.*;
import com.coffee.soft.mapper.*;
import com.coffee.soft.service.ItemsDetailPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:LiuRuidong
 * @Description: 商品详情页的实现
 * @Date: Created in 16:55 2018/2/4
 * @Modified By:
 */
@Service
public class ItemsDatailPageServiceImpl implements ItemsDetailPageService {

    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private MyCollectionMapper myCollectionMapper;
    @Autowired
    private AssessMappe assessMappe;
    @Autowired
    private UsersAssessMapper usersAssessMapper;
    @Autowired
    private FootprintMapper footprintMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * @Author：LiuRuidong
     * @Description: 返回商品详情
     * @Date:2018/2/4 10:02
     * @Param: 用户名和商品名
     * @return  返回查询到的商品信息
     */
    @Override
    public ItemsDetail showItemsDatail(UsersItems usersItems) {
        com.coffee.soft.entity.ItemsDetail itemsDetail = new com.coffee.soft.entity.ItemsDetail();
        Items items = itemsMapper.loadById(usersItems.getItemsId());
        if(items!=null){
            itemsDetail.setItemsId(items.getItemsId());
            itemsDetail.setItemsName(items.getItemsName());
            itemsDetail.setItemsPrice(items.getItemsPrice());
            itemsDetail.setItemsPicture(items.getItemsPicture());
            itemsDetail.setItemsInfo(items.getItemsInfo());
            itemsDetail.setItemsDiscountPrice(items.getItemsPrice()*items.getItemsDiscount());
            itemsDetail.setMyCollectionStatus(myCollectionMapper.selectMyCollectionStatus(usersItems));
            itemsDetail.setItemsPraise((assessMappe.goodCount(usersItems.getItemsId())*100)/assessMappe.allCount(usersItems.getItemsId()));
        }else{
            new RuntimeException("该商品不存在");
        }
        return itemsDetail;
    }
    /**
     * @Author:LiuRuidong
     * @Description: 实现商品评价信息功能
     * @Date: Created in 10:42 2018/2/4
     * @Modified By:
     */
    @Override
    public List<UserAssess> ItemsUsersAssess(UsersItems usersItems) {
        return usersAssessMapper.selectItemsAssess(usersItems.getItemsId());
    }
    /**
     * @Author：LiuRuidong
     * @Description: 给前台返回收藏状态
     * @Date:2018/2/4 11:27
     * @Param:
     * @return
     */
    @Override
    public int MyCollectionStatus(UsersItems usersItems) {

        int status=0;
// 判断是否未收藏，第一个参数表示收藏状态，第二个是判断表中有没有
//      未收藏时（表中没有||状态为0）
        if (myCollectionMapper.selectMyCollectionStatus(usersItems)==0|| myCollectionMapper.selectMyCollectionItems(usersItems)==null){
// 判断表中有没有
            //          有时
            if(myCollectionMapper.selectMyCollectionItems(usersItems)!=null){
                //                将该用户的该收藏状态置位1
                myCollectionMapper.updateItemsStatusMyCollection(usersItems);
                status=1;
                //          没有
            }else{
                //              用户收藏车中的数量<20,表示收藏车未满
                if(myCollectionMapper.selectMyCollectionItemsCount(usersItems.getUserId())<20){
                    //                  将该用户的该商品添加到收藏表中去
                    myCollectionMapper.insertItemsMyCollection(usersItems);
                    status = 1;
                }else{
                    //                   该用户的该收藏表已满
                    status = -1;
                }
            }
            //       已收藏时
        }else{
            //          取消收藏
            myCollectionMapper.updateCancelMyCollection(usersItems);
        }
        return status;
    }
    /**
     * @Author:LiuRuidong
     * @Description: 返回足迹表中的状态
     * @Date: Created in 11:54 2018/2/4
     * @Modified By:
     */
    @Override
    public void MyFootprintService(UsersItems usersItems) {
//      判断我的足迹表是否存在
//        存在
        if(footprintMapper.selectMyFootprintItems(usersItems)!=null){
            footprintMapper.updateItemsUpdateTime(usersItems);
        }else{
            footprintMapper.insertItemsMyFootprintItems(usersItems);
        }
    }
    /**
     * @Author:LiuRuidong
     * @Description: 购物车的实现类
     * @Date: Created in 14:59 2018/2/4
     * @Modified By:
     */
    @Override
    public Boolean shoppingCart(UsersItems usersItems) {

//       查询用户的购物车是否已满
        if(shoppingCartMapper.selectShoppingItemsCount(usersItems.getUserId())<20){
//            查询购物车中该用户的该商品是否存在
//                    存在
            if(shoppingCartMapper.selectShoppingCartItems(usersItems)!=null){
                int num = shoppingCartMapper.selectShoppingEnsureItemsCount(usersItems);
                ShoppingCart shoppingCart = new ShoppingCart();
                shoppingCart.setItemsId(usersItems.getItemsId());
                shoppingCart.setUserId(usersItems.getUserId());
                shoppingCart.setShoppingItemsCount(num+1);
                shoppingCartMapper.updateItemsShoppingCart(shoppingCart);
//                不存在时
            }else{
                shoppingCartMapper.insertItemsShoppingCart(usersItems);
            }
        }else{
            return false;
        }
        return true;
    }


}
